var request = require('request');

var FeatureParser = function(text, opt, callback){
    request.post('http://172.21.213.224:8761/ltp', {form: {s: text, x : 'n', t : 'a'}}, function(err, data){
        if(err){
            return callback(err);
        }
        var resJSON = JSON.parse(data.body);
        var wordsColl = resJSON[0][0];
        var GeoFeatures = {
            Time : [],
            Place : [],
            Person : [],
            Thing : [],
            Event : [],
            Phenomenon : [],
            Scene : [],
            TagType : opt.Collection,
            TagId : opt._id,
            meta : opt.MetaID
        };
        for(var i = 0; i < wordsColl.length; i++){
            if (wordsColl[i].pos == 'nt'){
                GeoFeatures.Time.push(wordsColl[i].cont);
            }
            else if(wordsColl[i].pos == 'ns'){
                GeoFeatures.Place.push(wordsColl[i].cont);
            }
            else if(wordsColl[i].pos == 'nh'){
                GeoFeatures.Person.push(wordsColl[i].cont);
            }
            else if(wordsColl[i].pos == 'v'){
                GeoFeatures.Event.push(wordsColl[i].cont);
            }
            else if(wordsColl[i].pos == 'n'){
                if(wordsColl[i].cont.indexOf('风') > -1||wordsColl[i].cont.indexOf('水') > -1||wordsColl[i].cont.indexOf('山') > -1||wordsColl[i].cont.indexOf('土') > -1||wordsColl[i].cont.indexOf('气') > -1||wordsColl[i].cont.indexOf('川') > -1||wordsColl[i].cont.indexOf('火') > -1||wordsColl[i].cont.indexOf('冰') > -1||wordsColl[i].cont.indexOf('蒸') > -1){
                    GeoFeatures.Phenomenon.push(wordsColl[i].cont);
                }
                else{
                    GeoFeatures.Thing.push(wordsColl[i].cont);
                }
            }
        }
        var scene = '';
        for (var i = 0; i < GeoFeatures.Place.length; i++){
            scene += GeoFeatures.Place[i];
        }
        if (scene.trim() != ''){
            GeoFeatures.Scene.push(scene);
        }
        return callback(null, GeoFeatures);
    });
}

module.exports = FeatureParser;